package com.genexususa.eo.connection.google.main;

import com.genexususa.eo.connection.google.analytics.AnalyticsManager;
import com.genexususa.eo.connection.google.analytics.Top10KeywordsResult;
import com.genexususa.eo.connection.google.analytics.Top10KeywordsResultItem;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by genexus on 12/2/2014.
 */
public class getPageViewsDayByDay {

    public static void main(String[] args){

        try{

            String year = "2014";
            String month = "11";

            String profileId = "26486741";

            int endDay = 01;

            ArrayList<String> startDates = new ArrayList<String>();

            for (int i = 1; i <= endDay; i++){
                if (i < 10) {
                    String date = year + "-" + month + "-0" + i;
                    startDates.add(date);
                }else{
                    String date = year + "-" + month + "-" + i;
                    startDates.add(date);
                }
            }
            String xml = "\n" +
                    "<GoogleAnalyticsQuery xmlns=\"L2TMediaTrunk\">\n" +
                    "        <filePath>c:\\l2t\\gm</filePath>\n" +
                    "        <applicationName>L2TMEDIA</applicationName>\n" +
                    "        <profileId>%s</profileId>\n" +
                    "        <startDate>%s</startDate>\n" +
                    "        <endDate>%s</endDate>\n" +
                    "        <maxResults>10000</maxResults>\n"+
                    "        <accountId/>\n" +
                    "        <Query/>\n" +
                    "        <metrics>\n" +
                    "                <Item>ga:visits</Item>\n" +
                    "                <Item>ga:visitors</Item>\n" +
                    "                <Item>ga:pageviews</Item>\n" +
                    "                <Item>ga:timeOnSite</Item>\n" +
                    "                <Item>ga:newVisits</Item>\n" +
                    "                <Item>ga:bounces</Item>\n" +
                    "        </metrics>\n" +
                    "                <dimensions>\n" +
                    "                               <Item>ga:keyword</Item>\n" +
                    "                </dimensions>\n" +
                    "        <segments>\n" +
                    "                <Item>gaid::-5</Item>\n" +
                    "        </segments>\n" +
                    "</GoogleAnalyticsQuery>\n";

            String result = "";

            for (int i = 0; i < startDates.size(); i++){
                String data = String.format(xml, profileId, startDates.get(i), startDates.get(i));
                AnalyticsManager am = new AnalyticsManager();
                String res = am.getTop10Keywords(data);

                JAXBContext jc = JAXBContext.newInstance(Top10KeywordsResult.class);

                Unmarshaller unmarshaller = jc.createUnmarshaller();
                StringReader xmlObj = new StringReader(res);
                Top10KeywordsResult tests = (Top10KeywordsResult) unmarshaller.unmarshal(xmlObj);

                Marshaller marshaller = jc.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.marshal(tests, System.out);
                Top10KeywordsResult keywordsResult = new Top10KeywordsResult();
                Top10KeywordsResultItem item = new Top10KeywordsResultItem();
                for (int j = 0; j < tests.Results.size(); j++){

                    item.TotalPageviews += tests.Results.get(j).TotalPageviews;
                    item.TotalUniqueVisitors += tests.Results.get(j).TotalUniqueVisitors;
                    item.TotalVisits += tests.Results.get(j).TotalVisits;
                    item.TimeOnSite += tests.Results.get(j).TimeOnSite;
                    item.NewVisits += tests.Results.get(j).NewVisits;
                    item.Bounces += tests.Results.get(j).Bounces;
                }

                keywordsResult.Results.add(item);

                String resultItem = "";
                try {

                    JAXBContext contextObj = JAXBContext.newInstance(Top10KeywordsResult.class);

                    Marshaller marshallerObj = contextObj.createMarshaller();
                    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

                    StringWriter sw = new StringWriter();

                    marshallerObj.marshal(keywordsResult, sw);
                    resultItem = sw.getBuffer().toString();



                } catch (Exception ex) {
                    ex.printStackTrace();
                    resultItem = ex.getMessage();

                }

                //Top10KeywordsResult resObj =
                result += "-------------" + startDates.get(i) + "----------------------\n";
                result += res + "\n\n";
                //System.out.println(res);
            }

            writeXMLFile("c:\\l2tSync\\B", profileId, result);


//segment=gaid::-10
          /*  AnalyticsManager am = new AnalyticsManager();
            String res = am.getTop10Keywords(xml);
            System.out.println(res);




            System.out.println("");*/
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    private static void writeXMLFile(String filePath, String profileId, String xml){
        try {

            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("MMddYYYY");
            String fileName = filePath + "_" + dateFormat.format(date) + "_" + profileId + ".xml";

            FileWriter fw = new FileWriter(fileName);
            BufferedWriter bw = new BufferedWriter(fw);

            bw.write(xml);
            bw.flush();
            bw.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}
